// pages/releaseSetting/releaseSetting.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    receiveDic:null,
    tagArr: [{
        ids: '200',
        name: '职场',
      checked: false
      },
      {
        ids: '100',
        name: '管理',
        checked: false
      },
      {
        ids: '600',
        name: '健身',
        checked: false
      },
      {
        ids: '1000',
        name: '旅行',
        checked: false
      },
      {
        ids: '900',
        name: '趣闻',
        checked: false
      },
      {
        ids: '400',
        name: '生活',
        checked: false
      },
      {
        ids: '800',
        name: '育儿',
        checked: false
      },
      {
        ids: '300',
        name: '美食',
        checked: false
      }
    ],
    priceArr: [{
      ids: '0',
      name: '免费',
      checked: true
    },
      {
        ids: '1.00',
        name: '¥ 1.00',
        checked: false
      },
      {
        ids: '3.00',
        name: '¥ 3.00',
        checked: false
      },
      {
        ids: '5.00',
        name: '¥ 5.00',
        checked: false
      },
      {
        ids: '9.00',
        name: '¥ 9.00',
        checked: false
      },
      {
        ids: '19.00',
        name: '¥ 19.00',
        checked: false
      }]
  },
   //选择价格  单选 传的是对应ids
  selectPrice:function(event){
    console.log('当前第几个' + event.currentTarget.dataset.idx)
    for (var i = 0; i < this.data.priceArr.length; i++) {
      if (event.currentTarget.dataset.idx == i) {
        this.data.priceArr[i].checked = true
      }
      else {
        this.data.priceArr[i].checked = false
      }
    }
    console.log(this.data.priceArr)
    this.setData(this.data)
  },
  //选择标签  多选 传的是对应ids
  selectTag:function(event){
    console.log('当前id' + event.currentTarget.dataset.idx)
    var that = this;
    var tempArr = that.data.tagArr;
    var tempClickDic = tempArr[event.currentTarget.dataset.idx];
    tempClickDic.checked = !tempClickDic.checked;
    tempArr.splice(event.currentTarget.dataset.idx, 1, tempClickDic);//指定位置替换掉数据
    that.setData({
      tagArr: tempArr
    })
  },

  //确定
  handlerSureTap:function(){
        //map 遍历数组 获取对应的标签id 和name
    var selectTarIDArr = new Array, selectTarNameArr = new Array;
    this.data.tagArr.map((item,index)=>{
      if (item.checked) {
        selectTarIDArr.push(item.ids)
        selectTarNameArr.push(item.name)
      }
    })

    if (selectTarIDArr.length > 3){
      wx:wx.showToast({
        title: '最多选择3个标签',
        icon: 'none',
      })
      return;
    }

    var selectPrice = '0';//默认免费0
    this.data.priceArr.map((item, index) => {
      if (item.checked) {
        selectPrice = item.ids
      }
    })
    console.log(selectTarIDArr, selectTarNameArr)
    console.log('价格' + selectPrice)

    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2]; //上一个页面
    prevPage.setData({
      //处理上个页面的dataArr
      selectTarIDArr: selectTarIDArr,
      selectTarNameArr: selectTarNameArr,
      price: selectPrice
    })
    wx.navigateBack({
      delta: 1,
    })
    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log("接收到的参数是obj=" + options.receiveDic);//此处打印出来的仅仅是字符串 需要解析，解析如下
    this.setData({
      receiveDic: JSON.parse(options.receiveDic),
    })

    this.data.tagArr.map((item, index) => {
      this.data.receiveDic.selectTarIDArr.map((itemSelect, indexSelect) => {
        if (item.ids == itemSelect) {
          item.checked = true
        }
      })
    })

    this.data.priceArr.map((item, index) => {
      if (item.ids == this.data.receiveDic.price) {
          item.checked = true
        }
    })

    this.setData(this.data)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})